package com.learn.disruptor.basic.handler;

import cn.hutool.core.util.StrUtil;
import com.learn.disruptor.basic.event.LongEvent;
import com.lmax.disruptor.EventHandler;
import com.lmax.disruptor.Sequence;

/**
 * @author huangChao
 **/
public record LongEventHandler(Integer num, java.util.concurrent.CountDownLatch countDownLatch)
    implements EventHandler<LongEvent>
{

    /**
     * 启动
     */
    @Override
    public void onStart() {
        EventHandler.super.onStart();
    }

    /**
     * 批拉取启动
     * @param batchSize 一次拉取数量
     * @param queueDepth 队列深度
     */
    @Override
    public void onBatchStart(long batchSize, long queueDepth) {
        EventHandler.super.onBatchStart(batchSize, queueDepth);
    }

    @Override
    public void onShutdown() {
        EventHandler.super.onShutdown();
    }

    @Override
    public void onTimeout(long sequence) throws Exception {
        EventHandler.super.onTimeout(sequence);
    }

    /**
     * 事件处理方法
     *
     * @param event      待处理事件
     * @param sequence   当前处理序列(自然顺序)
     * @param endOfBatch 是否为当前处理批的最后一个
     */
    @Override
    public void onEvent(LongEvent event, long sequence, boolean endOfBatch) throws Exception {
        System.out.println(StrUtil.join(" -> ", Thread.currentThread().getName(), num, event.getId(), sequence, endOfBatch));
        countDownLatch.countDown();
    }

    @Override
    public void setSequenceCallback(Sequence sequenceCallback) {
        EventHandler.super.setSequenceCallback(sequenceCallback);
    }

}
